/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * CommonUtil.java created on 2020年2月6日 上午9:51:09 by Lyon Lu 
 */
package com.mvc.server.miaosha.common;

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

/**
 * <pre>
 * Description:
 * 
 * @author Lyon Lu
 * @date 2020年2月6日 上午9:51:09
 *
 * </pre>
 */
public class CommonUtil {
	
	public static String beanToString(Object value) {
		if(value == null) {
			return null;
		}
		Class<?> clazz = value.getClass();
		if(clazz == int.class || clazz == Integer.class) {
			 return "" + value;
		}else if(clazz == String.class) {
			 return (String)value;
		}else if(clazz == long.class || clazz == Long.class) {
			return "" + value;
		}else {
			return JSON.toJSONString(value);
		}
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T stringToBean(String str, Class<T> clazz) {
		if(StringUtils.isBlank(str) || Objects.isNull(clazz)) {
			 return null;
		}
		if(clazz == int.class || clazz == Integer.class) {
			 return (T) Integer.valueOf(str);
		}else if(clazz == String.class) {
			 return (T)str;
		}else if(clazz == long.class || clazz == Long.class) {
			return  (T)Long.valueOf(str);
		}else {
			return JSON.toJavaObject(JSON.parseObject(str), clazz);
		}
	}
}
